package com.zy.demo048084.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/nacos")
@RequiredArgsConstructor
public class NacosController {

    private final DiscoveryClient client;

    @GetMapping("/info")
    public List<Map<String, Object>> getServicesInfo() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> services = client.getServices();
        services.forEach(serviceName -> {
            List<ServiceInstance> instances = client.getInstances(serviceName);
            instances.forEach(instance -> {
                Map<String, Object> map = new HashMap<>();
                map.put("serviceName", serviceName);
                map.put("serviceId", instance.getServiceId());
                map.put("host", instance.getHost());
                list.add(map);
            });
        });
        return list;
    }
}
